﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Smart.Navigation.Windows.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public class ControlViewProvider : IViewProvider
    {
        private readonly ContentControl parent;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parent"></param>
        public ControlViewProvider(ContentControl parent)
        {
            this.parent = parent;
        }

        /// <summary>
        ///
        /// </summary>
        public virtual bool IsAsync
        {
            get { return true; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="method"></param>
        /// <param name="args"></param>
        public void BeginInvoke(Delegate method, params object[] args)
        {
            this.parent.Dispatcher.BeginInvoke( method, args );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public object ViewCreate(Type type)
        {
            Control view = (Control)Activator.CreateInstance( type );

            this.parent.Content = view;

            return view;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="view"></param>
        public void ViewActive(object view)
        {
            Control control = (Control)view;

            control.Visibility = Visibility.Visible;
            control.Focus();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="view"></param>
        public void ViewDeactive(object view)
        {
            Control control = (Control)view;

            control.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="view"></param>
        public void ViewDispose(object view)
        {
        }
    }
}
